/**
 * Created by TOPFEEL on 2018/2/24.
 */

layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;

    //表单重新渲染
    form.render();

    //表格渲染
    var tableObj = table.render({
        id: 'list_table'
        , elem: '#list_table'
        , url: 'sys/role/list'
        , align: "center"
        , cols: [[ //表头
            {type: 'checkbox'}
            , {type: 'numbers', title: '序号', width: 50}
            , {field: 'roleName', title: '角色名称', width: 140, align: 'center'}
            , {
                field: 'roleState', title: '状态', width: 70, align: 'center', templet: function (d) {
                    return d.roleState == 1 ? '<span class="font-success">启用</span>' : '<span class="font-danger">禁用</span>';
                }
            }
            // , {field: 'isSystemName', title: '系统权限', width: 100, align: 'center'}
            , {field: 'createTime', title: '添加时间', width: 170, align: 'center'}
            , {field: 'remark', title: '备注', width: 200, align: 'center'}
            // , {field: 'lock', title: '权限', width: 100, templet: '#ID_authorization', unresize: true, align: 'center'}
            // , {field: 'lock', title: '操作', width: 100, templet: '#ID_detail', unresize: true, align: 'center'}
            // , {
            //     field: 'lock', title: '权限', width: 80, templet: function (d) {
            //         return d.isSystem == '1' ? '<button class="layui-btn layui-btn-warm layui-btn-sm layui-btn-disabled" disabled="disabled" lay-event="authorization">授权</button>' : '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="authorization">授权</button>';
            //     }, unresize: true, align: 'center'
            // }
            , {
                field: 'lock', title: '操作', width: 150, templet: function (d) {
                    return d.isSystem == '1' ?
                        '<button class="layui-btn layui-btn-warm layui-btn-sm layui-btn-disabled" disabled="disabled" lay-event="detail">修改</button>' +'<button class="layui-btn layui-btn-warm layui-btn-sm layui-btn-disabled"  lay-event="authorization">授权</button>':/*disabled="disabled"*/
                        '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="detail">修改</button>'+'<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="authorization">授权</button>';
                }, unresize: true, align: 'center'
            }
        ]]
    });

    //监听工具条
    table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值

        if (layEvent === 'detail') {

            showDialog({
                title: '修改'
                , template: 'role-edit'
                , saveUrl: 'sys/role/save'
                , tableObj: tableObj
                , width: 500
                , height: 400
                , htmlData: data
            })
        } else if (layEvent === 'authorization') {

            showDialog({
                title: '授权'
                , template: 'role-auth'
                , saveUrl: 'sys/role/authorization'
                , tableObj: tableObj
                , width: 400
                , height: 500
                , htmlData: data
            })
        }
    });

    $("#role_add").click(function () {
        showDialog({
            title: '添加'
            , template: 'role-add'
            , saveUrl: 'sys/role/save'
            , tableObj: tableObj
            , width: 500
            , height: 400
        })
    });

    $("#role_del").click(function () {
        var data = table.checkStatus('list_table').data;
        var arr = [];
        for (var i = 0; i < data.length; i++) {
            if (data[i].isSystem != 1) {
                arr.push(data[i].roleId);
            }
        }
        if (data.length == 0) {
            layer.msg("请至少选择一条");
            return;
        }
        if (arr.length == 0) {
            layer.msg("系统数据禁止删除");
            return;
        }
        showConfirm('确认删除？', function () {
            $.ajax({
                type: "GET",
                url: 'sys/role/delete',
                data: {"roleId": arr.join(",")},
                success: function (result) {
                    if (result.success) {
                        layer.msg("删除成功");
                        reloadTable(tableObj);
                    } else {
                        layer.msg("删除异常");
                    }
                },

            })
        }, 0);
    });


    //查询数据
    $('#query').on('click', function () {
        reloadTable(tableObj);
    });

});